package com.ts.api.http.org.base;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.asymmetric.RSA;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.base.entity.apply.BaseOrgApplyQingYu;
import com.ts.api.http.org.base.entity.apply.BaseOrgApplyQingYuData;
import com.ts.api.http.org.base.entity.apply.BaseOrgDataExchangeMaskQingYu;
import com.ts.api.http.org.utils.ApiQingYuUtils;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.List;

/**
 * 庆鱼八位
 * @author WJB
 */
public class ApiOrgPhoneMaskQingYu2 implements ApiOrgPhoneMaskConvertHandler {
    private final String accessKey;
    private final String publicKey;
    public final String t7;

    public ApiOrgPhoneMaskQingYu2(String accessKey, String publicKey, String t7) {
        this.accessKey = accessKey;
        this.publicKey = publicKey;
        this.t7 = t7;
    }

    @Override
    public String dataExchange(ApiDataExchangeParameter apiDataChangeMessage) {
        BaseOrgDataExchangeMaskQingYu dataExchangeHX = new BaseOrgDataExchangeMaskQingYu();
        dataExchangeHX.setAccessKey(accessKey);
        dataExchangeHX.setRequestId(apiDataChangeMessage.getReqId());
        RSA rsa = new RSA(publicKey, null);
        String phone = apiDataChangeMessage.getPhoneMask();
        dataExchangeHX.setPhone(rsa.encryptToBase64Str(phone));

        return JsonUtils.toJson(dataExchangeHX);
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        return RespOrgPhoneMaskDataExchange.ofSuc();
    }

    @Override
    public String apply(ApiApplyParameter ApiApplyMessage) throws Exception {
        BaseOrgApplyQingYu apply = new BaseOrgApplyQingYu();
        apply.setAccessKey(accessKey);
        apply.setRequestId(ApiApplyMessage.getReqId());

        List<BaseOrgApplyQingYuData> list = CollUtils.newArrayList(newList -> {
            BaseOrgApplyQingYuData applyData = new BaseOrgApplyQingYuData();
            applyData.setT1(ApiApplyMessage.getPhone());
            applyData.setT2(ApiApplyMessage.getUserName());

            //  客户类型
            applyData.setT3("1");
            applyData.setT7(t7);
            //  贷款额度
            if (ValidParam.isNotEmpty(ApiApplyMessage.getLoanAmount())) {
                String money = ChooseEq.create()
                        .when(ApiApplyMessage.getLoanAmount(), "1", "30000")
                        .when(ApiApplyMessage.getLoanAmount(), "2", "50000")
                        .when(ApiApplyMessage.getLoanAmount(), "3", "100000")
                        .when(ApiApplyMessage.getLoanAmount(), "4", "200000")
                        .end();
                applyData.setT4(money);
            }

            String areaCode = ApiApplyMessage.getAreaCode();
            //  省编码
            applyData.setT8(areaCode.substring(0, 2) + "0000");
            //  市编码
            applyData.setT9(areaCode);
            //  拓展信息
            applyData.setT10(getT10Text(ApiApplyMessage));
            //  加密数据
            ApiQingYuUtils.encryptData(applyData, publicKey);

            newList.add(applyData);
        });
        apply.setData(list);

        String sendJson = JsonUtils.toJson(apply);
        return sendJson;
    }


    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 200);
        if (!code) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }

    public static String getT10Text(ApiApplyParameter apiApplyMessage) {
        StringBuilder stringBuilder = new StringBuilder();

        if (ValidParam.isNotEmpty(apiApplyMessage.getAge())) {
            String money = ChooseEq.create()
                    .when(apiApplyMessage.getLoanAmount(), "1", "30000")
                    .when(apiApplyMessage.getLoanAmount(), "2", "50000")
                    .when(apiApplyMessage.getLoanAmount(), "3", "100000")
                    .when(apiApplyMessage.getLoanAmount(), "4", "200000")
                    .end();
            stringBuilder.append("借款额度：").append(money);
        }

        //  年龄
        if (ValidParam.isNotEmpty(apiApplyMessage.getAge())) {
            stringBuilder.append("年龄：").append(apiApplyMessage.getAge()).append(";").append("\n");
        }

        //  性别
        if (ValidParam.isNotEmpty(apiApplyMessage.getSex())) {
            String sex = ChooseEq.create()
                    .when(apiApplyMessage.getSex(), "0", "男")
                    .when(apiApplyMessage.getSex(), "1", "女")
                    .end("");
            stringBuilder.append("性别：").append(sex).append(";").append("\n");
        }

        //  信用记录-逾期记录
        if (ValidParam.isNotEmpty(apiApplyMessage.getOverdue())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getOverdue(), "1", "无逾期")
                    .when(apiApplyMessage.getOverdue(), "2", "有逾期")
                    .when(apiApplyMessage.getOverdue(), "3", "一年以内无逾期")
                    .end();
            stringBuilder.append("信用记录：").append(text).append(";").append("\n");
        }

        //  芝麻分
        if (ValidParam.isNotEmpty(apiApplyMessage.getSesameCredit())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getSesameCredit(), "1", "600分以下")
                    .when(apiApplyMessage.getSesameCredit(), "2", "600~649分")
                    .when(apiApplyMessage.getSesameCredit(), "3", "650~699分")
                    .when(apiApplyMessage.getSesameCredit(), "4", "700分以上")
                    .end();
            stringBuilder.append("芝麻分：").append(text).append(";").append("\n");
        }

        //  职业
        if (ValidParam.isNotEmpty(apiApplyMessage.getCareer())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getCareer(), "1", "上班族")
                    .when(apiApplyMessage.getCareer(), "2", "自由职业")
                    .when(apiApplyMessage.getCareer(), "3", "企业主（有营业执照）")
                    .when(apiApplyMessage.getCareer(), "4", "公务员或事业单位")
                    .end();
            stringBuilder.append("职业：").append(text).append(";").append("\n");
        }

        //  学历
        if (ValidParam.isNotEmpty(apiApplyMessage.getEducationAttainment())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getEducationAttainment(), "1", "初中及以下")
                    .when(apiApplyMessage.getEducationAttainment(), "2", "中专")
                    .when(apiApplyMessage.getEducationAttainment(), "3", "高中")
                    .when(apiApplyMessage.getEducationAttainment(), "4", "大专")
                    .when(apiApplyMessage.getEducationAttainment(), "5", "本科")
                    .when(apiApplyMessage.getEducationAttainment(), "6", "研究生及以上")
                    .end();
            stringBuilder.append("学历：").append(text).append(";").append("\n");
        }

        //  收入形式
        if (ValidParam.isNotEmpty(apiApplyMessage.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getSalaryPayMethod(), "1", "银行代发打卡")
                    .when(apiApplyMessage.getSalaryPayMethod(), "2", "现金收入")
                    .when(apiApplyMessage.getSalaryPayMethod(), "3", "个人转账")
                    .end();
            stringBuilder.append("收入形式：").append(text).append(";").append("\n");
        }

        //  客户类型
        String career = apiApplyMessage.getCareer();
        if (ValidParam.isEmpty(career)) {
            //  无数据为个人用户
            stringBuilder.append("客户类型：").append("个人").append(";").append("\n");
        } else {
            String text = ChooseEq.create()
                    //  如果是企业就为企业用户
                    .when(career, "3", "企业")
                    //  个人用户
                    .end("个人");
            stringBuilder.append("客户类型：").append(text).append(";").append("\n");
        }

        //  房产情况
        if (ValidParam.isNotEmpty(apiApplyMessage.getRealEstateInfo())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getRealEstateInfo(), "1", "无")
                    .end("有");
            stringBuilder.append("房产情况：").append(text).append(";").append("\n");
        }

        //  车产
        if (ValidParam.isNotEmpty(apiApplyMessage.getCarInfo())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getCarInfo(), "1", "无")
                    .end("有");
            stringBuilder.append("车辆情况：").append(text).append(";").append("\n");
        }

        //  保单
        if (ValidParam.isNotEmpty(apiApplyMessage.getInsurancePolicy())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getInsurancePolicy(), "1", "无")
                    .end("有");
            stringBuilder.append("保单：").append(text).append(";").append("\n");
        }

        //  社保
        if (ValidParam.isNotEmpty(apiApplyMessage.getSocialSecurity())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getSocialSecurity(), "1", "无")
                    .end("有");
            stringBuilder.append("社保：").append(text).append(";").append("\n");
        }

        //  公积金
        if (ValidParam.isNotEmpty(apiApplyMessage.getProvidentFund())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getProvidentFund(), "1", "无")
                    .end("有");
            stringBuilder.append("公积金：").append(text).append(";").append("\n");
        }

        //  银行代发
        if (ValidParam.isNotEmpty(apiApplyMessage.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiApplyMessage.getSalaryPayMethod(), "1", "是")
                    .end("否");
            stringBuilder.append("银行代发：").append(text).append(";").append("\n");
        }
        return stringBuilder.toString();
    }
}

